package cn.net.chenbao.medical;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanManager;
import android.device.scanner.configuration.PropertyID;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.OnDismissListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.net.chenbao.medical.base.BaseActivity;
import cn.net.chenbao.medical.bean.Patient;
import cn.net.chenbao.medical.dialog.TwoSelectDialog;
import cn.net.chenbao.medical.mvp.FunctionP;
import cn.net.chenbao.medical.mvp.presenter.IFunctionP;
import cn.net.chenbao.medical.mvp.view.IFunctionV;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.util.DialogUtils;
import cn.net.chenbao.medical.util.MyViewUtils;
import cn.net.chenbao.medical.util.TimeUtil;
import cn.net.chenbao.medical.util.WWToast;
import cn.net.chenbao.medical.util.ZLog;
import cn.net.chenbao.medical.weight.CommonDialog;

public class FunctionActivity extends BaseActivity implements View.OnClickListener, IFunctionV, Handler.Callback {
    private TextView mTvTitle, mTvSure, mTvDate, mTvSelect;
    private LinearLayout mLlTitleRight;
    private int mode;

    private TextView mTvType, mTvYpsm, mTvSicknessName, mTvBedNumber, mTvDrugMethod, mTvSpeed, mTvNumber, mTvRow;
    private TextView mTvPatientName, mTvPatientAge, mTvPatientBunber, mTvPatientTime;
    private TextView mTvJc, mTvPz, mTvCc, mTvJj;

    private LinearLayout mLlPrescription, mLlDrugContainer;
    private CommonDialog mDialog, mDialogTwo;

    private TextView mTvCheckPrint;

    private boolean isFlag;//确定回车键事件
    private CheckBox mCb;

    private TextView mTvFinish;

    private IFunctionP mFunctionP;

    private String mDate;
    private int mPyFlag = 1;//1.配液 0、2.空包
    private long mPrintId = 1;//配液批次

    private TimePickerView mTimePV;
    private OptionsPickerView mPcPv;

    private Calendar mSelectCalendar;
    private SimpleDateFormat mSDF;

    private TwoSelectDialog mTwoSelectDialog;

    private Patient mPatient;

    private boolean mIsFinish;//是否结束去查询报表


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function);
        mFunctionP = new FunctionP(this);
        InitTitle();
        initView();
//        doData(mode);
    }

    @SuppressLint("SimpleDateFormat")
    private void InitTitle() {
        mSDF = new SimpleDateFormat("yyyy-MM-dd");
        mSelectCalendar = Calendar.getInstance();

        mode = getIntent().getIntExtra(Consts.KEY_MODULE, -1);
        findViewById(R.id.iv_left).setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_center);
        mTvSure = (TextView) findViewById(R.id.tv_sure);
        mTvFinish = (TextView) findViewById(R.id.tv_finish);
        mTvFinish.setOnClickListener(this);
        mTvDate = (TextView) findViewById(R.id.tv_date);
        mLlTitleRight = (LinearLayout) findViewById(R.id.ll_right);
        mTvSelect = (TextView) findViewById(R.id.tv_select);
        mCb = (CheckBox) findViewById(R.id.cb);

        switch (mode) {
            case Consts.MODE_NURSE: {
                mTvTitle.setText(R.string.nurse_sure);
                mTvSure.setText(R.string.sure_transfusion);
                mTvFinish.setVisibility(View.VISIBLE);
                mLlTitleRight.setVisibility(View.VISIBLE);
                mLlTitleRight.setOnClickListener(this);//日期可选一次
                mDate = mSDF.format(mSelectCalendar.getTime());
                mTvSelect.setText(String.format(getString(R.string.print_id), mPrintId + ""));
                mTvDate.setText(mDate);
//                showPcPicker();
                break;
            }
            case Consts.MODE_KBCC:
                //空包出仓流程与出仓一致
                mPyFlag = 2;
                mTvTitle.setText(R.string.kb_apothecary_sure);
                mTvSure.setText(R.string.sure_take_out);
                mTvFinish.setVisibility(View.VISIBLE);
                mLlTitleRight.setVisibility(View.VISIBLE);
                mLlTitleRight.setOnClickListener(this);//日期可选一次
                mDate = mSDF.format(mSelectCalendar.getTime());
                mTvSelect.setText(String.format(getString(R.string.print_id), mPrintId + ""));
                mTvDate.setText(mDate);
                showPcPicker();
                break;
            case Consts.MODE_APOTHECARY: {
                mTvTitle.setText(R.string.apothecary_sure);
                mTvSure.setText(R.string.sure_take_out);
                mTvFinish.setVisibility(View.VISIBLE);
                mLlTitleRight.setVisibility(View.VISIBLE);
                mLlTitleRight.setOnClickListener(this);//日期可选一次
                mDate = mSDF.format(mSelectCalendar.getTime());
                mTvSelect.setText(String.format(getString(R.string.print_id), mPrintId + ""));
                mTvDate.setText(mDate);
                showPcPicker();
                break;
            }
            case Consts.MODE_DRUG_WITHDRAWAL: {
                mTvTitle.setText(R.string.drug_withdrawal);
                mTvSure.setText(R.string.sure_withdrawal);
                break;
            }
            case Consts.MODE_HSJJ:
                mTvTitle.setText(R.string.nurse_connect);
                mTvSure.setText(R.string.nurse_connect_sure);
                mTvFinish.setVisibility(View.VISIBLE);
                mLlTitleRight.setVisibility(View.VISIBLE);
                if (mPyFlag != 1) {
                    mTvSelect.setText(String.format(getString(R.string.transfuse_name), "空包"));
                } else {
                    mTvSelect.setText(String.format(getString(R.string.transfuse_name), "配液"));
                }
                mDate = mSDF.format(mSelectCalendar.getTime());
                mTvDate.setText(mDate);
                showTwoDialog();
                break;
            case Consts.MODE_JC:
                mTvTitle.setText(R.string.jc);
                mTvSure.setText(R.string.jc_sure);
                mTvFinish.setVisibility(View.VISIBLE);
                mLlTitleRight.setOnClickListener(this);//日期可选一次
                mSelectCalendar.add(Calendar.DAY_OF_MONTH, 1);
                mDate = mSDF.format(mSelectCalendar.getTime());
                mLlTitleRight.setVisibility(View.VISIBLE);
                mTvSelect.setText(String.format(getString(R.string.print_id), mPrintId + ""));
                mTvDate.setText(mDate);
                showPcPicker();
                break;
        }
    }

    private void initView() {
        mLlDrugContainer = (LinearLayout) findViewById(R.id.ll_drug_container);
        mLlPrescription = (LinearLayout) findViewById(R.id.ll_prescription);
        findViewById(R.id.ll_scan).setOnClickListener(this);
        findViewById(R.id.tv_sure).setOnClickListener(this);
        mTvType = (TextView) findViewById(R.id.tv_type);
        mTvYpsm = (TextView) findViewById(R.id.tv_ypsm);
        mTvSicknessName = (TextView) findViewById(R.id.sickness_name);
        mTvBedNumber = (TextView) findViewById(R.id.tv_bed_number);
        mTvDrugMethod = (TextView) findViewById(R.id.tv_drug_method);
        mTvSpeed = (TextView) findViewById(R.id.tv_speed);
        mTvNumber = (TextView) findViewById(R.id.tv_number);
        mTvRow = (TextView) findViewById(R.id.tv_row);

        mTvPatientName = (TextView) findViewById(R.id.tv_patient_name);
        mTvPatientAge = (TextView) findViewById(R.id.tv_patient_age);
        mTvPatientBunber = (TextView) findViewById(R.id.tv_patient_bunber);
        mTvPatientTime = (TextView) findViewById(R.id.tv_patient_time);

        mTvCheckPrint = (TextView) findViewById(R.id.tv_check_print);
        mTvJc = (TextView) findViewById(R.id.tv_jc);
        mTvPz = (TextView) findViewById(R.id.tv_pz);
        mTvCc = (TextView) findViewById(R.id.tv_cc);
        mTvJj = (TextView) findViewById(R.id.tv_jj);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left: {
                finish();
                break;
            }
            case R.id.ll_scan: {
                doScan();
                break;
            }
            case R.id.ll_right:
                showPcPicker();
                break;
            case R.id.tv_sure: {
                SubmitInfo(mode);
                break;
            }
            case R.id.tv_finish:
                mIsFinish = true;
                if (mode == Consts.MODE_NURSE)
                    showPcPicker();
                else
                    showTimePicker();
                break;
        }
    }

    private Intent mIntent;

    private void intent(int mode) {
        mIntent = mIntent == null ? new Intent(this, ReportListActivity.class) : mIntent;
        mIntent.putExtra(Consts.KEY_MODULE, mode);
        switch (mode) {
            case Consts.MODE_NURSE:
            case Consts.MODE_JC:
            case Consts.MODE_KBCC:
            case Consts.MODE_APOTHECARY:
                //进仓报表
                mIntent.putExtra("startDate", mDate);
                mIntent.putExtra("printId", mPrintId);
                break;
            case Consts.MODE_HSJJ:
                //交接报表
                mIntent.putExtra("startDate", mDate);
                mIntent.putExtra("pyFlag", mPyFlag);
                break;
        }
        startActivity(mIntent);
    }

    private void SubmitInfo(final int mode) {
        switch (mode) {
            case Consts.MODE_DRUG_WITHDRAWAL: {
                showExitDrug();
                break;
            }
            default:
                mFunctionP.doSubmitInfo(mode, mScanResult, mPrintId, mPyFlag, mPatient == null ? -1 : mPatient.Printid);
        }
    }


    @Override
    public void SykGetSuccess(Patient patient) throws Exception {
        ShowData(patient);
    }

    @Override
    public void SubmitSuccess() {
        WWToast.showShort(getString(R.string.submit_success));
    }


    private Handler mHandler;
    private Runnable mRunnable;

    @Override
    public void SubmitFail(String msg) {
        if (mDialogTwo == null) {
            mDialogTwo = DialogUtils
                    .getSingleBtnDialog(FunctionActivity.this,
                            msg, "好", true);
            mDialogTwo.getButtonRight().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialogTwo.dismiss();
                }
            });
            mDialogTwo.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {
                        if (mDialogTwo != null && mDialogTwo.isShowing()) {
                            isFlag = true;
                            mDialogTwo.dismiss();
                        }
                    }
                    return false;
                }
            });
            mDialogTwo.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (mHandler != null) {
                        mHandler.removeCallbacks(mRunnable);
                    }
                }
            });
        }
        mDialogTwo.setContentText(msg);
        mDialogTwo.show();
        if (mHandler == null) {
            mHandler = new Handler(this);
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(0);
                }
            };
        }
        mHandler.postDelayed(mRunnable, 2000);
    }

    @Override
    public boolean handleMessage(Message msg) {
        mDialogTwo.dismiss();
        return false;
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        ZLog.e(event.getKeyCode() + "");
        if (KeyEvent.ACTION_DOWN == event.getAction()) {
            switch (event.getKeyCode()) {
                case 19:
                    //上
                    if (mPcPv != null && mPcPv.isShowing()) {
                        mPcPv.SetOption1CurrentItemPre(10);
                        break;
                    }
                    if (mTimePV != null && mTimePV.isShowing()) {
                        mSelectCalendar.add(Calendar.DAY_OF_MONTH, -1);
                        mTimePV.setEventDate(mSelectCalendar);
                    }
                    break;
                case 20:
                    //下
                    if (mPcPv != null && mPcPv.isShowing()) {
                        mPcPv.SetOption1CurrentItemNext(10);
                        break;
                    }
                    if (mTimePV != null && mTimePV.isShowing()) {
                        mSelectCalendar.add(Calendar.DAY_OF_MONTH, 1);
                        mTimePV.setEventDate(mSelectCalendar);
                    }
                    break;
                case 6:
                    //S2键
                    if (mTwoSelectDialog != null && mTwoSelectDialog.isShowing())
                        break;
                    if (mPcPv != null && mPcPv.isShowing())
                        break;
                    if (mTimePV != null && mTimePV.isShowing())
                        break;
                    mIsFinish = true;
                    if (mode == Consts.MODE_NURSE)
                        showPcPicker();
                    else
                        showTimePicker();
                    break;
            }
            return super.dispatchKeyEvent(event);
        }

        //这里注意要作判断处理，ActionDown、ActionUp都会回调到这里，不作处理的话就会调用两次
        if (KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_UP == event.getAction()) {
            if (mode == Consts.MODE_DRUG_WITHDRAWAL) {
                //退药
                if (isFlag) {
                    mFunctionP.doSubmitInfo(mode, mScanResult, mPrintId, mPyFlag, mPatient == null ? -1 : mPatient.Printid);
                    isFlag = false;
                } else {
                    showExitDrug();
                }
            } else {
                //处理事件
                if (mPcPv != null && mPcPv.isShowing()) {
                    mPcPv.returnData();
                    mPcPv.dismiss();
                    return true;
                }
                if (isFlag) {
                    isFlag = false;
                    return true;
                }
                if (mTimePV != null && mTimePV.isShowing()) {
                    mTimePV.returnData();
                    mTimePV.dismiss();
                    return true;
                }

                if (!isFlag) {
                    mFunctionP.doSubmitInfo(mode, mScanResult, mPrintId, mPyFlag, mPatient == null ? -1 : mPatient.Printid);
                }
                isFlag = false;
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


    void showExitDrug() {
        if (mDialog == null) {
            mDialog = DialogUtils
                    .getCommonDialogTwiceConfirm(FunctionActivity.this,
                            R.string.exit_drug, true);
            mDialog.getButtonRight().setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDialog.dismiss();
                            mFunctionP.doSubmitInfo(mode, mScanResult, mPrintId, mPyFlag, mPatient == null ? -1 : mPatient.Printid);
                        }
                    });
            mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {
                        if (mDialog != null && mDialog.isShowing()) {
                            isFlag = true;
                            mDialog.dismiss();
                        }
                    }
                    return false;
                }
            });
        }
        mDialog.show();
    }

    /*******************扫描的相关方法***********************/
    private final static String SCAN_ACTION = ScanManager.ACTION_DECODE;//default action
    private ScanManager mScanManager;
    private boolean isScaning;
    private SoundPool soundpool = null;
    private Vibrator mVibrator;
    private int soundid;
    private long mScanResult = -1;

    private void doScan() {
        isScaning = true;
        mScanManager.startDecode();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initScan();
        mVibrator = mVibrator == null ? (Vibrator) getSystemService(Context.VIBRATOR_SERVICE) : mVibrator;
        IntentFilter filter = new IntentFilter();
        int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG};
        String[] value_buf = mScanManager.getParameterString(idbuf);
        if (value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
            filter.addAction(value_buf[0]);
        } else {
            filter.addAction(SCAN_ACTION);
        }
        registerReceiver(mScanReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mScanManager != null) {
            mScanManager.stopDecode();
            isScaning = false;
        }
        unregisterReceiver(mScanReceiver);
    }

    private void initScan() {
        // TODO Auto-generated method stub
        mScanManager = new ScanManager();
        mScanManager.openScanner();

        mScanManager.switchOutputMode(0);
        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
    }

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            isScaning = false;
            soundpool.play(soundid, 1, 1, 0, 0, 1);

            mVibrator.vibrate(100);

            byte[] barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
            int barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
            byte temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, (byte) 0);
            android.util.Log.i("debug", "----codetype--" + temp);
            String result = new String(barcode, 0, barcodelen);
            mScanResult = Long.parseLong(result);
            mFunctionP.doScanData(mScanResult);
        }

    };


    private void ShowData(Patient patient) throws Exception {
        mScanManager.stopDecode();
        if (patient == null)
            return;
        this.mPatient = patient;
        if (!TextUtils.isEmpty(patient.PyFlag) && TextUtils.equals("1", patient.PyFlag)) {
            mCb.setChecked(true);
        } else {
            mCb.setChecked(false);
        }
        mTvType.setText(String.format(getResources().getString(R.string.name_type), patient.Printid + "", patient.PrintRow == null ? "" : patient.PrintRow));
        mLlPrescription.setVisibility(View.VISIBLE);
        mTvYpsm.setText(patient.Ypsm == null ? "" : patient.Ypsm);

        mTvSicknessName.setText(patient.KsmcName == null ? "" : patient.KsmcName);

        mTvBedNumber.setText(patient.Cw == null ? "" : patient.Cw);

        mTvDrugMethod.setText(String.format(getString(R.string.method), patient.YfName == null ? "" : patient.YfName, patient.YfId == null ? "" : patient.YfId));
        mTvSpeed.setText(String.format(getString(R.string.speed), patient.Syds1 == null ? "" : patient.Syds1.trim()));
        mTvNumber.setText(patient.ComNo == null ? "" : patient.ComNo);
        mTvRow.setText(patient.PrintRow == null ? "" : patient.PrintRow);

        mTvPatientName.setText(patient.Xm == null ? "" : patient.Xm);
        mTvPatientAge.setText(patient.Age == null ? "" : patient.Age);
        mTvPatientBunber.setText(patient.Zyh == null ? "" : patient.Zyh);

        mTvCheckPrint.setText(patient.Sfdy == null ? "" : patient.Sfdy);
        mTvJc.setText(patient.JcOperName == null ? "" : patient.JcOperName);
        mTvPz.setText(patient.RealOperName == null ? "" : patient.RealOperName);
        mTvCc.setText(patient.OutOperName == null ? "" : patient.OutOperName);
        mTvJj.setText(patient.HsjjOperName == null ? "" : patient.HsjjOperName);

        mTvPatientTime.setText(String.format(getString(R.string.name_time), TimeUtil.timeToData(patient.Rq)));
        mLlDrugContainer.removeAllViews();
        if (!TextUtils.isEmpty(patient.Yp1)) {
            View view = View.inflate(this, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp1);
            tvDrugSpecifition.setText("" + patient.Jl1);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl1 / patient.Jl1));
            tvDrugDosage.setText("" + patient.Jlyl1);
            tvDrugUnit.setText(patient.JldwName1 == null ? "" : patient.JldwName1);

            tvDrugBatchNumber.setText(String.format(getResources().getString(R.string.batch_number), patient.PhYp1 == null ? "" : patient.PhYp1));
            tvDrugValidity.setText(String.format(getResources().getString(R.string.validity), patient.VaildDate1 == null ? "" : TimeUtil.timeToData(patient.VaildDate1)));
            mLlDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp2)) {
            View view = View.inflate(this, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp2);
            tvDrugSpecifition.setText("" + patient.Jl2);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl2 / patient.Jl2));
            tvDrugDosage.setText("" + patient.Jlyl2);
            tvDrugUnit.setText(patient.JldwName2 == null ? "" : patient.JldwName2);
            tvDrugBatchNumber.setText(String.format(getResources().getString(R.string.batch_number), patient.PhYp2 == null ? "" : patient.PhYp2));
            tvDrugValidity.setText(String.format(getResources().getString(R.string.validity), patient.VaildDate2 == null ? "" : TimeUtil.timeToData(patient.VaildDate2)));
            mLlDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp3)) {
            View view = View.inflate(this, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp3);
            tvDrugSpecifition.setText("" + patient.Jl3);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl3 / patient.Jl3));
            tvDrugDosage.setText("" + patient.Jlyl3);
            tvDrugUnit.setText(patient.JldwName3 == null ? "" : patient.JldwName3);

            tvDrugBatchNumber.setText(String.format(getResources().getString(R.string.batch_number), patient.PhYp3 == null ? "" : patient.PhYp3));

            tvDrugValidity.setText(String.format(getResources().getString(R.string.validity), patient.VaildDate3 == null ? "" : TimeUtil.timeToData(patient.VaildDate3)));
            mLlDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp4)) {
            View view = View.inflate(this, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp4);
            tvDrugSpecifition.setText("" + patient.Jl4);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl4 / patient.Jl4));
            tvDrugDosage.setText("" + patient.Jlyl4);
            tvDrugUnit.setText(patient.JldwName4 == null ? "" : patient.JldwName4);

            tvDrugBatchNumber.setText(String.format(getResources().getString(R.string.batch_number), patient.PhYp4 == null ? "" : patient.PhYp4));

            tvDrugValidity.setText(String.format(getResources().getString(R.string.validity), patient.VaildDate4 == null ? "" : TimeUtil.timeToData(patient.VaildDate4)));
            mLlDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp5)) {
            View view = View.inflate(this, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp5);
            tvDrugSpecifition.setText("" + patient.Jl5);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl5 / patient.Jl5));
            tvDrugDosage.setText("" + patient.Jlyl5);
            tvDrugUnit.setText(patient.JldwName5 == null ? "" : patient.JldwName5);
            tvDrugBatchNumber.setText(String.format(getResources().getString(R.string.batch_number), patient.PhYp5 == null ? "" : patient.PhYp5));
            tvDrugValidity.setText(String.format(getResources().getString(R.string.validity), patient.VaildDate5 == null ? "" : TimeUtil.timeToData(patient.VaildDate5)));
            mLlDrugContainer.addView(view);
        }
    }

    @Override
    public void showPcPicker() {
        if (mPcPv == null) {
            final List<Long> pcList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                pcList.add((long) (i + 1));
            }
            mPcPv = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    mPrintId = pcList.get(options1);
                    mTvSelect.setText(String.format(getString(R.string.print_id), mPrintId + ""));
                    //修改
                    if (mode == Consts.MODE_NURSE)
                        showTimePicker();
                }
            })
                    .setDividerColor(ContextCompat.getColor(this, R.color.main_line_color))
                    .setTextColorCenter(ContextCompat.getColor(this, R.color.main_color)) //设置选中项文字颜色
                    .setTextColorOut(ContextCompat.getColor(this, R.color.black2))
                    .setCancelColor(ContextCompat.getColor(this, R.color.black))
                    .setSubmitColor(ContextCompat.getColor(this, R.color.main_color))
                    .setTitleText("选择批次")
                    .setTitleSize(13)
                    .setTitleBgColor(ContextCompat.getColor(this, R.color.white))
                    .setBgColor(ContextCompat.getColor(this, R.color.white))
                    .setContentTextSize(16)
                    .setSubCalSize(16)
                    .build();
            mPcPv.setPicker(pcList);
        }
        mPcPv.show();
    }


    @Override
    public void showTwoDialog() {
        if (mTwoSelectDialog == null) {
            mTwoSelectDialog = new TwoSelectDialog(
                    FunctionActivity.this, R.style.DialogStyle);
            mTwoSelectDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mPyFlag = mTwoSelectDialog.getResult();
                    if (mPyFlag != 1) {
                        mTvSelect.setText(String.format(getString(R.string.transfuse_name), "空包"));
                        //空包默认显示当天日期+1 否则显示当天日期
                        mSelectCalendar.add(Calendar.DAY_OF_MONTH, 1);
                    } else {
                        mTvSelect.setText(String.format(getString(R.string.transfuse_name), "配液"));
                    }
                    mDate = mSDF.format(mSelectCalendar.getTime());
                    mTvDate.setText(mDate);
                    showTimePicker();
                }
            });

            mTwoSelectDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    ZLog.e(keyCode + "");
                    if (KeyEvent.ACTION_DOWN == event.getAction()) {
                        switch (keyCode) {
                            case 19:
                            case 20:
                                //上 下键
                                if (mTwoSelectDialog != null && mTwoSelectDialog.isShowing()) {
                                    mTwoSelectDialog.setSex(!mTwoSelectDialog.getResult2());
                                }
                                break;
                        }
                    }
                    if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {
                        isFlag = true;
                        mTwoSelectDialog.dismiss();
                    }
                    return false;
                }
            });
        }
        mTwoSelectDialog.show();
    }


    @Override
    public void showTimePicker() {
        if (mTimePV == null) {
            //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
            //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
            Calendar startDate = Calendar.getInstance();
            startDate.set(mSelectCalendar.get(Calendar.YEAR) - 5, 0, 1);
            Calendar endDate = Calendar.getInstance();
            endDate.set(mSelectCalendar.get(Calendar.YEAR) + 3, 11, 30);
            //时间选择器
            mTimePV = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {//选中事件回调
                    // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                    mDate = mSDF.format(date);
                    if (mIsFinish) {
                        intent(mode);
                        finish();
                    } else {
                        mTvDate.setText(mDate);
//                        mSelectCalendar.setTime(date);
                    }
                }
            })
                    //年月日时分秒 的显示与否，不设置则默认全部显示
                    .setType(new boolean[]{true, true, true, false, false, false})
                    .setLabel("", "月", "日", "", "", "")
                    .isCenterLabel(false)
                    .setDividerColor(ContextCompat.getColor(FunctionActivity.this, R.color.main_line_color))
                    .setContentSize(15)
                    .setTitleText("选择日期")
                    .setTitleSize(13)
                    .setDate(mSelectCalendar)
                    .setRangDate(startDate, endDate)
                    .setBgColor(0xFFFFFFFF)
                    .setTitleBgColor(0xFFFFFFFF)
                    .setSubCalSize(15)
                    .setCancelText("取消")
                    .setSubmitColor(ContextCompat.getColor(FunctionActivity.this, R.color.main_color))
                    .setCancelColor(ContextCompat.getColor(FunctionActivity.this, R.color.black))
                    .setTextColorOut(ContextCompat.getColor(FunctionActivity.this, R.color.black2))
                    .setTextColorCenter(ContextCompat.getColor(this, R.color.main_color)) //设置选中项文字颜色
                    .setDecorView(null)
                    .build();
            mTimePV.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(Object o) {
                    mIsFinish = false;
                }
            });
            mTimePV.show();
        } else {
            mTimePV.show();
        }
    }

}
